package com.neuedu.nep.service.impl;

import com.neuedu.nep.dao.AqiFeedbackDao;
import com.neuedu.nep.dao.AqiFeedbackMapper;
import com.neuedu.nep.dao.StatisticDao;
import com.neuedu.nep.dto.AdminPageDTO;
import com.neuedu.nep.model.Aqi;
import com.neuedu.nep.model.AqiFeedback;
import com.neuedu.nep.model.Statistic;
import com.neuedu.nep.service.AqiFeedBackService;
import com.neuedu.nep.service.AqiService;
import com.neuedu.nep.vo.FeedBackInfoVO;
import com.neuedu.nep.vo.PageVO;
import com.neuedu.nep.vo.ShowFeedbackVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 黄宇佳
 * @create 2024-04-10 16:09
 * @description:
 */
@Service
public class AqiFeedBackServiceImpl implements AqiFeedBackService {
    @Resource
    private AqiFeedbackMapper aqiFeedbackMapper;

    @Resource
    private AqiFeedbackDao aqiFeedbackDao;

    @Resource
    private StatisticDao statisticDao;

    private static final Object LOCKED = new Integer(0);

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int addAqiFeedBack(AqiFeedback aqiFeedback) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format=simpleDateFormat.format(new Date());
        String[] s=format.split(" ");
        String Date=s[0];
        String Time=s[1];
        aqiFeedback.setAfDate(Date);
        aqiFeedback.setAfTime(Time);
        aqiFeedback.setState(0);
        int result=0;
        synchronized (LOCKED){
            result+=aqiFeedbackDao.insertSelective(aqiFeedback);
            Statistic statistic=new Statistic();
            statistic.setProvinceId(aqiFeedback.getProvinceId());
            statistic.setCityId(aqiFeedback.getCityId());
            statistic.setAddress(aqiFeedback.getAddress());
            statistic.setInformation(aqiFeedback.getInformation());
            statistic.setFdId(aqiFeedback.getTelId());
            result+=statisticDao.insertSelective(statistic);
        }
        return result;
    }

    @Override
    public List<ShowFeedbackVO> page(String telId, Integer lastItemId, Integer pageSize) {
        List<ShowFeedbackVO> showFeedbackVOS = aqiFeedbackMapper.pageFeedbackVO(pageSize, lastItemId, telId);
        return showFeedbackVOS;
    }

    @Override
    public List<ShowFeedbackVO> page(Integer gmId, Integer lastItemId, Integer pageSize) {
        List<ShowFeedbackVO> showFeedbackVOS = aqiFeedbackMapper.pageFeedbackVOByGmId(pageSize, lastItemId, gmId);
        return showFeedbackVOS;
    }

    @Override
    public FeedBackInfoVO getFeedBackInfo(Integer afId) {
        FeedBackInfoVO feedbackInfo = aqiFeedbackMapper.getFeedbackInfo(afId);
        return feedbackInfo;
    }

    @Override
    public PageVO<ShowFeedbackVO> page(AdminPageDTO adminPageDTO) {
        Integer provinceId=adminPageDTO.getProvinceId();
        Integer cityId=adminPageDTO.getCityId();
        Integer estimatedGrade=adminPageDTO.getEstimatedGrade();
        String afDate=adminPageDTO.getAfDate();
        Integer state=adminPageDTO.getState();
        String assignDate=adminPageDTO.getAssignDate();
        Integer limitParam1=(adminPageDTO.getPageNo()-1)*adminPageDTO.getPageSize();
        Integer limitParam2=adminPageDTO.getPageSize();
        List<ShowFeedbackVO> showFeedbackVOS = aqiFeedbackMapper.pageFeedbackVOForAdmin(provinceId,
                cityId,
                estimatedGrade,
                afDate,
                state,
                assignDate,
                limitParam1,
                limitParam2);
        Integer count = aqiFeedbackMapper.pageFeedbackVOForAdminCount(provinceId,
                cityId,
                estimatedGrade,
                afDate,
                state,
                assignDate);
        PageVO<ShowFeedbackVO> pageVO=new PageVO<>();
        pageVO.setData(showFeedbackVOS);
        pageVO.setPageNo(adminPageDTO.getPageNo());
        pageVO.setPageSize(adminPageDTO.getPageSize());
        int pageCount=count/adminPageDTO.getPageSize()+(count%adminPageDTO.getPageSize()==0?0:1);
        pageVO.setPageCount(pageCount);
        pageVO.setCount(count);
        return pageVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int setGmIdToFeedback(Integer afId, Integer gmId) {
        AqiFeedback aqiFeedback=new AqiFeedback();
        aqiFeedback.setAfId(afId);
        aqiFeedback.setGmId(gmId);
        aqiFeedback.setState(1);
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        String[] s = format.split(" ");
        String assignDate=s[0];
        String assignTime=s[1];
        aqiFeedback.setAssignDate(assignDate);
        aqiFeedback.setAssignTime(assignTime);

        int result=0;
        result+=aqiFeedbackDao.updateByPrimaryKeySelective(aqiFeedback);

        Statistic statistic=new Statistic();
        statistic.setId(afId);
        statistic.setGmId(gmId);
        result+=statisticDao.updateByPrimaryKeySelective(statistic);
        return result;
    }


}
